﻿var index = 0;
var div12 = undefined;
var ip = "192.168.0.10";

$(document).ready(function() {
	getIP();
	setInterval(upgrade, 5 * 1000);
})

function upgrade() {
	window.control.upgrade();
}
function getIP(){
	ip = window.control.readFiles();
	$("#versionName").html("当前版本号:v1.0.2(20180118)");
    setTimeout("searchPage()", 1000);
    $('.carousel').carousel({
        interval: 8000
    });
}
function searchPage() {
	$.ajax({
		type : "post",
  	    async:false,
  	    cache : false,
  	    url : "http://" + ip + "/andStatus",
  	    dataType : "jsonp",
  	    jsonpCallback:"successCallback",
  	    timeout : 30000,
		scriptCharset: 'utf-8',
		success : function(data, textStatus) {
            $("#allDetail").html("");
            var allDetail = $("#allDetail");
            $("#bottomPage").html("");
            var bottomPage = $("#bottomPage");
			var length = data.length;
            var page = Math.ceil(length/10);
            for(var i=0;i<page;i++){
                var item = document.createElement("div");
                var li = document.createElement("li");
                if(i == 0){
                    item.setAttribute("class","item active");
                    li.setAttribute("class","active");
                }else{
                    item.setAttribute("class","item");
                }
                li.setAttribute("data-target","#myCarousel");
                li.setAttribute("data-slide-to",i);
                bottomPage.append(li);

                var table = document.createElement("table");
                table.setAttribute("class", "table");
                var thead = document.createElement("thead");
                thead.setAttribute("class", "thead");
                thead.innerHTML = '<tr>' +
                '<th width="115px">设备名称</th>'+
                '<th width="192px">车辆自编号</th>'+
                '<th width="192px">员工卡号</th>'+
                '<th width="112px">当前SOC</th>'+
                '<th width="112px">已充电量</th>'+
                '<th width="192px">已充时间</th>'+
                '<th width="115px">输出电压</th>'+
                '<th width="115px">输出电流</th>'+
                '<th width="115px">输出功率</th>'+
                '</tr>';
                table.appendChild(thead);
                var tbody = document.createElement("tbody");
                for(var j=0; j<length && j<10; j++){
                    index = i*10 + j;
                    if(length<=0){
                    } else {
                        div12 = document.createElement("tr");
                        div12.setAttribute("id","message"+index);
                        tbody.appendChild(div12);
                    }
                }
                length = length - 10;

                table.appendChild(tbody);
                item.appendChild(table);
                allDetail.append(item);
			}
            search();
		},
		error: function(data){
			alert("查询失败");
		},
		timeout: function(data){
			alert("查询超时");
		}
	});
}

function search() {
    $.ajax({
        type : "post",
  	    async:false,
  	    cache : false,
        url : "http://" + ip + "/andStatus",
        dataType : "jsonp",
        jsonpCallback:"successCallback",
        timeout : 30000,
        scriptCharset: 'utf-8',
        success : function(data, textStatus) {
            var length = data.length;
            for (var i = 0; i < length; i++) {
                if (data[i].deviceState == "充电中") {
                    var $tr = $("#message" + i);
					$tr.empty();
                    $("<td>").html('<img src="../static/images/charging.png" height="30" width="30"/>' + (i+1) + "号桩").appendTo($tr);
                    $("<td>").html(data[i].carNumber).appendTo($tr);
                    $("<td>").html(data[i].cardNo).appendTo($tr);
                    $("<td>").html('<span style="color:#34f504; font-size: 29px;">' + data[i].CurrentSOC + '</span><span style="color:#34f504; font-size: 16px;">%</span>').appendTo($tr);
                    $("<td>").html(round(data[i].chargedElectricity, 2)).appendTo($tr);
                    $("<td>").html(data[i].realChargedDTTM).appendTo($tr);
                    $("<td>").html(data[i].outputVoltage).appendTo($tr);
                    $("<td>").html(data[i].outputelectric).appendTo($tr);
                    $("<td>").html(round(data[i].outputVoltage * data[i].outputelectric / 1000, 2)).appendTo($tr);
                } else if (data[i].deviceState == "充电结束") {
                    $("#message" + i).empty();
					$("#message" + i).html('' +
                        '<td><img src="../static/images/after-charge.png" height="30" width="30"/>' + (i+1) + '号桩</td>' +
                        '<td>' + data[i].carNumber + '</td>' +
                        '<td>' + data[i].cardNo + '</td>' +
                        '<td><span style="color:#34f504; font-size: 29px;">' + data[i].CurrentSOC + '</span><span style="color:#34f504; font-size: 16px;">%</span></td>' +
                        '<td>' + round(data[i].chargedElectricity, 2) + '</td>' +
                        '<td colspan="4" style="border:0px; text-align: right;">' +
                        '<strong class="equipmentState blue">充电结束，待拔枪！</strong>' +
                        '</td>');
                } else if (data[i].ChargingGun1 == "已连接" && data[i].deviceState != "充电中") {
                    $("#message" + i).empty();
					$("#message" + i).html('' +
                        '<td><img src="../static/images/before-charge.png" height="30" width="30"/>' + (i+1) + '号桩</td>' +
                        '<td colspan="8" style="border:0px; text-align: left; padding-left: 52px">' +
                        '<strong class="equipmentState blue">已连接，待充电！</strong>' +
                        '</td>');
                } else if (data[i].communicationState == "断开") {
					$("#message" + i).empty();
                    $("#message" + i).html('' +
                        '<td><img src="../static/images/guzhang.png" height="30" width="30"/>' + (i+1) + '号桩</td>' +
                        '<td colspan="8" style="border:0px; text-align: left; padding-left: 52px">' +
                        '<strong class="equipmentState red">通讯故障，请检查！</strong>' +
                        '</td>');
                } else {
					$("#message" + i).empty();
                    $("#message" + i).html('' +
                        '<td><img src="../static/images/kongxian.png" height="30" width="30"/>' + (i+1) + '号桩</td>' +
                        '<td colspan="8" style="border:0px; text-align: left; padding-left: 52px">' +
                        '<strong class="equipmentState blue">空闲中…</strong>' +
                        '</td>');
                }
            }
        },
        error: function(data){
        },
        timeout: function(data){
        }
    });
    setTimeout("search()", 1000);
}